#include "ThreadPool.h"
void getinvertdirname(char* dirname){
    int low=0;
    int high=strlen(dirname)-1;
    while(low<high){
        char tmp=dirname[low];
        dirname[low]=dirname[high];
        dirname[high]=tmp;
        low++,high--;
    }
}

bool isDir(int id,MYSQL* mysql){
    char sqlDir[4096];
    bzero(sqlDir,sizeof(sqlDir));
    sprintf(sqlDir,"select type from virtual_file where id=%d;",id);
    int ret=mysql_query(mysql,sqlDir);
    MYSQL_QUERY_CHECK(ret,mysql);
    MYSQL_RES* result=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    if(row[0][0]=='d'){

        return true;//表示是文件夹
    }
    else{

        return false;//表示不是文件夹
    }
}

bool isTomb(int id,MYSQL* mysql){

    char sqlTomb[4096];
    bzero(sqlTomb,sizeof(sqlTomb));
    sprintf(sqlTomb,"select tomb from virtual_file where id=%d;",id);
    int ret=mysql_query(mysql,sqlTomb);
    MYSQL_QUERY_CHECK(ret,mysql);
    MYSQL_RES* result=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    if(strcmp(row[0],"1")==0){

        return true;//表示已经被删除
    }
    else{

        return false;//表示未被删除
    }
}

void idToPath(int id,char* path,MYSQL* mysql){
    char sqlId[4096];
    bzero(sqlId,sizeof(sqlId));
    sprintf(sqlId,"select path from virtual_file where id=%d;",id);
    int ret=mysql_query(mysql,sqlId);
    MYSQL_QUERY_CHECK(ret,mysql);

    MYSQL_RES* result=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    if(strcmp(row[0],"/")!=0){
        strcpy(path,row[0]);
    }
}
int mkdirFunc(int id_now,const char* arg,char* user,MYSQL* mysql){
    int retval=0;
    char str[1024];
    printf("arg is %s\n",arg);
    strcpy(str,arg);
    int idx=0;
    char* p[1024];
    p[idx++]=strtok(str," ");
    while((p[idx++]=strtok(NULL," "))!=NULL);//存入p数组将各个参数
    for(int i=0;i<idx && p[i]!=NULL;i++){
        //h获取文件夹的名字
        char dirname[1024]={0};
        char pathname[1024]={0};
        strcpy(pathname,p[i]);
        int len=strlen(pathname);
        printf("pathname is %s\n",pathname);
        for(int k=len-1,h=0;k>=0;k--,h++){
            if(pathname[k]!='/'){
                
                dirname[h]=pathname[k];
                pathname[k]='\0';
            }
            else{

                break;
            }
        }
        //获取正常dirname
        //
        printf("dirname before is:%s \n",dirname);
        getinvertdirname(dirname);
        printf("dirname is: %s\n",dirname);//看看是不是正过来了
        printf("pathname is: %s\n",pathname);

        //传给getAbsolutePath的name是去掉了要创建的文件夹的名字
        int id = getAbsolutePath(id_now,pathname,user,mysql);
        printf("id from getAbsolutePath: %d\n",id);
        if(id==-1){

            retval=-1;
            break;
        }
        //如果id对应的是文件，则创建失败；
        if(!isDir(id,mysql))
        {
            printf("创建的路径是文件，错误\n");
            retval = -1;
            break;
        }
        if(isTomb(id,mysql)){
            //表示文件夹早已被删除，那么就是无
            //效
            printf("创建的文件路径包含已被删除路径，错误\n");
            retval=-1;
            break;
        }
        char pathdir[1024];
        bzero(pathdir,sizeof(pathdir));
        idToPath(id,pathdir,mysql);//活动要存放的目录地址
        
        printf("pathdir is: %s\n",pathdir);
        char sqlInsert[4096];
        bzero(sqlInsert,sizeof(sqlInsert));
        //curPath存储创建目录成功之后path的内容
        if(strcmp(pathdir,"/") != 0){
            strcat(pathdir,"/");
        }
        strcat(pathdir,dirname);
        printf("pathdir now is :%s\n",pathdir);
        sprintf(sqlInsert,"insert into virtual_file (name,type,pre_id,path,user,md5,tomb) values('%s','d',%d,'%s','%s',null,%d)",dirname,id,pathdir,user,0);
        int qret = mysql_query(mysql,sqlInsert);
        MYSQL_QUERY_CHECK(qret,mysql);

    }
    return retval;
}
